/*******************************************************************************************
*文件:    S7protocol.h
*描述:    S7协议数据结构定义
*
*
*作者:    刘涛
*日期:    2017-03-28
*修改:    创建文件                            ------>     2017-03-28
*         修改定义                            ------>     2017-03-30
*         修改程序结构，进行代码分层处理      ------>     2017-05-05
*         增加TPKT和COTP检查                  ------>     2017-05-11
*
*******************************************************************************************/
#ifndef __APP_LAYER_S7_ISO_ON_TCP_H__
#define __APP_LAYER_S7_ISO_ON_TCP_H__

#include "datatype.h"

#pragma pack(push, 1)

//s7comm header+parameter
typedef struct _s7commstate {

    //s7coomm header
    uint8 protocol_id;     //s7协议标识：0x32
    uint8 rosctr;          //rosctr类型
    uint16 reserved;        //保留字
    uint16 data_unit_reference; //数据单元保留字
    uint16 para_len;    //参数长度
    uint16 data_len;    //数据长度

    //Parameter
    union {
        //job
        struct {
            uint8 job_func;   //function type字段
        } JOB;
        //ack和ack_data
        struct {
            uint8 error_class; //error class
            uint8 error_code;  //error code
            uint8 ack_func;   //function type字段
        } ACK;
        //user_data
        struct {
            uint32 param; //paramter head+length
            uint8 unknown;   //unknown
            uint8 type;       //type+functiong group type
            uint8 sub_func;   //subfunction
        } UD;
    } PARM;

} S7COMMSTATE, *PS7COMMSTATE;

#pragma pack(pop)

// Function codes in parameter part
#define S7COMM_SERV_CPU                     (0x00)
#define S7COMM_SERV_SETUPCOMM               (0xF0)
#define S7COMM_SERV_READVAR                 (0x04)
#define S7COMM_SERV_WRITEVAR                (0x05)

#define S7COMM_FUNC_REQUESTDOWNLOAD         (0x1A)
#define S7COMM_FUNC_DOWNLOADBLOCK           (0x1B)
#define S7COMM_FUNC_DOWNLOADENDED           (0x1C)
#define S7COMM_FUNC_STARTUPLOAD             (0x1D)
#define S7COMM_FUNC_UPLOAD                  (0x1E)
#define S7COMM_FUNC_ENDUPLOAD               (0x1F)
#define S7COMM_FUNC_PISERVICE               (0x28)
#define S7COMM_FUNC_PLCSTOP                 (0x29)

// Function codes in parameter part
#define PUSH_TYPE     (0)
#define REQUEST_TYPE  (4)
#define RESPONSE_TYPE (8)

//Names of Function groups in userdata parameter part
#define S7COMM_UD_FUNCGROUP_MODETRANS       (0x0)
#define S7COMM_UD_FUNCGROUP_PROG            (0x1)
#define S7COMM_UD_FUNCGROUP_CYCLIC          (0x2)
#define S7COMM_UD_FUNCGROUP_BLOCK           (0x3)
#define S7COMM_UD_FUNCGROUP_CPU             (0x4)
#define S7COMM_UD_FUNCGROUP_SEC             (0x5)    //Security functions e.g. plc password
#define S7COMM_UD_FUNCGROUP_PBC             (0x6)    // PBC = Programmable Block Communication (PBK in german)
#define S7COMM_UD_FUNCGROUP_TIME            (0x7)
#define S7COMM_UD_FUNCGROUP_NCPRG           (0xf)

//sub Function part
#define PC_VARTAB           (2)
#define PC_READDIAGDATA     (14)
#define BF_LISTBLOCKS       (1)
#define BF_LISTBLOCKSOfTYPE (2)
#define BF_GETBLOCKINFO     (3)
#define CF_READSZL          (1)
#define CF_SYSTEMSTATE      (2)
#define TF_READCLOCK        (1)
#define TF_SETCLOCK         (2)
#define CD_MEMORY           (1)

// PDU types
#define S7COMM_ROSCTR_JOB                   (0x01)
#define S7COMM_ROSCTR_ACK                   (0x02)
#define S7COMM_ROSCTR_ACK_DATA              (0x03)
#define S7COMM_ROSCTR_USERDATA              (0x07)

#endif
